import {
  selectPartsOfferList,
  selectPartsOfferDetail,
  selectPartsOfferInfo,
  savePartsOffer,
  selectFileList,
  savePartsOfferInfo,
  selectPartsDetail,
  deletePartsInfo,
} from '@/services/fleetManage/partsOffer';
import prompt from '@/components/Prompt';
import { getPageSize } from '@/utils/common';

export default {
  namespace: 'partsOffer',

  state: {
    partsOfferList: {}, // 配件报价列表
    partsOfferDetail: {}, // 配件报价详情
    partsOfferInfo: {}, // 配件报价明细
  },

  effects: {
    /**
     * 查询配件报价列表
     */
    *selectPartsOfferList({ payload, callback }, { call, put }) {
      const response = yield call(selectPartsOfferList, payload);
      const { code, data } = response;
      let pagination = { list: [], pageSize: getPageSize(), total: 0, pageNum: 1 };
      if (code === 0) {
        pagination = { ...pagination, ...data };
      }
      const { pageNum, pageSize, total, list } = pagination;
      yield put({
        type: 'saveAllValus',
        payload: {
          partsOfferList: {
            pagination: {
              current: pageNum,
              pageSize,
              total,
            },
            list,
          },
        },
      });
      callback && callback(list);
    },
    /**
     * 查询配件报价详情
     */
    *selectPartsOfferDetail({ payload, callback }, { call, put }) {
      const response = yield call(selectPartsOfferDetail, payload);
      const { code, data } = response;
      let newData = [];
      if (code === 0) {
        newData = data;
        const filePayLoad = { bizId: payload.id, fileBizType: 'PARTS_OFFER' };
        const fileResponse = yield call(selectFileList, filePayLoad);
        if (fileResponse.code === 0) {
          newData.fileList = fileResponse.data;
        } else {
          newData.fileList = [];
        }
      }
      yield put({
        type: 'detail',
        payload: { partsOfferDetail: { [payload.id]: newData } },
      });
      callback && callback(data);
    },

    /**
     *  查询配件报价明细
     */
    *selectPartsOfferInfo({ payload, callback }, { call, put }) {
      const response = yield call(selectPartsOfferInfo, payload);
      const { code, data } = response;
      let newList = [];
      if (code === 0) {
        newList = data;
      }
      yield put({
        type: 'detail',
        payload: {
          partsOfferInfo: {
            [payload.id]: {
              pagination: {},
              list: newList,
            },
          },
        },
      });
      callback && callback(newList);
    },
    /**
     * 新增编辑基本信息
     */
    *savePartsOffer({ payload, callback }, { call }) {
      const response = yield call(savePartsOffer, payload);
      const { code, data, message } = response;
      const content = payload.id ? `编辑${message}` : `新增${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(data);
    },
    /**
     * 提交并保存明细
     */
    *savePartsOfferInfo({ payload, callback }, { call }) {
      const response = yield call(savePartsOfferInfo, payload);
      const { code, data, message } = response;
      const content = payload.id !== -1 ? `编辑明细${message}` : `新增明细${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(true);
    },

    /**
     * 查看配件详情
     */
    *selectPartsDetail({ payload, callback }, { call }) {
      const response = yield call(selectPartsDetail, payload);
      const { code, data, message } = response;
      if (code !== 0) return;
      callback && callback(data);
    },

    /**
     * 删除明细
     */
    *deletePartsInfo({ payload, callback }, { call }) {
      const response = yield call(deletePartsInfo, payload);
      const { code, data, message } = response;
      if (code !== 0) return;
      prompt({ content: `明细删除${message}!` });
      callback && callback(true);
    },

    /**
     * 设置表单参数
     */
    *allValus({ payload }, { _, put }) {
      yield put({
        type: 'saveAllValus',
        payload,
      });
    },
  },

  reducers: {
    //设置表单参数
    saveAllValus(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    detail(state, { payload }) {
      let param = '';
      for (let k in payload) {
        param = k;
      }
      return {
        ...state,
        [param]: { ...state[param], ...payload[param] },
      };
    },
  },
};
